﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Globalization;
//using ACC_PCL_DataSetTableAdapters;
using System.Data.SqlClient;


/// <summary>
/// Summary description for Calendar
/// </summary>
public class Utility
{
	public Utility()
	{
		//
		// TODO: Add constructor logic here
        ////
        //DataSet ds = new ACC_PCL_DataSet();
        //ManagersTableAdapter ta  = new ManagersTableAdapter();
        //DataTable dt = ta.GetData();
       
       



        //ACC_PCL_DataSet.ACC_PROPERTIESSDataTable
	}

    public DataView Last3Months()
    {
 
        DataTable table = new DataTable();
        table.Columns.Add("Month", typeof(string));
        table.Columns.Add("Offset", typeof(int));

        DateTime dtStart;
        if (HttpContext.Current.Session["SalesDate"] != null)
            dtStart = (DateTime)HttpContext.Current.Session["SalesDate"];
        else
            dtStart = DateTime.Today.AddDays(-1);

        int nMonth = dtStart.Month;
        int nYear = dtStart.Year;

        for (int i = 0; i < 13; i++)

        {
            int j = nMonth - i; 
            if (j <= 0) j += 12;
            string sMonth = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(j);
            int iYear = dtStart.AddMonths(-i).Year;
            string sYear = iYear<nYear? " " + iYear.ToString().Remove(0,2):"";
            table.Rows.Add( sMonth + sYear,i);
        }
        return table.DefaultView;
    }

    public DataView Last3CalendarMonths()
    {

        DataTable table = new DataTable();
        table.Columns.Add("Month", typeof(string));
        table.Columns.Add("Offset", typeof(int));

        DateTime dtStart;
        dtStart = DateTime.Today;

        int nMonth = dtStart.Month;
        int nYear = dtStart.Year;

        for (int i = 0; i < 13; i++)
        {
            int j = nMonth - i;
            if (j <= 0) j += 12;
            string sMonth = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(j);
            int iYear = dtStart.AddMonths(-i).Year;
            string sYear = iYear < nYear ? " " + iYear.ToString().Remove(0, 2) : "";
            table.Rows.Add(sMonth + sYear, i);
        }
        return table.DefaultView;
    }

    public DataView CacheStats()
    {

        DataTable table = new DataTable();
        table.Columns.Add("CacheName", typeof(string));
        table.Columns.Add("DateCached", typeof(DateTime));
        table.Columns.Add("DateCacheUpdated", typeof(DateTime));
        table.Columns.Add("Rows", typeof(int));
        table.Columns.Add("Time", typeof(float));

        return table.DefaultView;
    }
}

public class Cachola
{
    DataSet _ds;

//This 
    public Cachola(string psCacheName)
    {

         _ds = (System.Data.DataSet)HttpContext.Current.Cache[psCacheName];
    }

    public Cachola()
    {
        throw new System.NotImplementedException();
    }

    public DataTable TableNamesList()
    {
        DataTable table = new DataTable();
        table.Columns.Add("TableName", typeof(string));
        if (_ds != null)
        {
            for (int i = 0; i < _ds.Tables.Count; i++)
            {
                table.Rows.Add(_ds.Tables[i].TableName);
            }
        }
        return table;
    }


    public void CacheNames()
    {
       
        int n = HttpContext.Current.Cache.Count;
        //for(int i=0;i<n;i++)
        //{
        //   object x = HttpContext.Current.Cache.GetEnumerator;
        //}


    }


    public DataTable Table(string psTableName)
    {
         return _ds.Tables[psTableName];
    }

    public DataTable Table(string psTableName,string psSort)
    {
        _ds.Tables[psTableName].DefaultView.Sort=psSort;

        return _ds.Tables[psTableName].DefaultView.Table;

    }
}